/**
 * 
 */
package com.eej.act.obj;

import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author DOIBALMI
 *
 */
public class EstropadakParser extends DefaultHandler {

	String temp = "";
	String anterior = "";
	String tagAnterior = "";
	ListaEstropada le = new ListaEstropada();
	Estropada estropada = null;
	int key;
	int dict = 0;
	
	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		temp += new String(ch,start,length);
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		//super.endElement(uri, localName, qName);
		//System.out.print(temp + "</" + qName + "> --> anterior: |" + anterior + "| tagAnterior = " + tagAnterior);
		if(qName.equals("string")){
			if(anterior.equals("IDestropada")){
				this.estropada.setIdEstropada(temp);
				//System.out.println(" dentro: " + this.estropada.getIdEstropada());
			}else if(anterior.equals("Izena")){
				this.estropada.setIzena(temp);
			}else if(anterior.equals("Lekua")){
				this.estropada.setLekua(temp);
			}else if(anterior.equals("Data")){
				this.estropada.setData(temp);
			}else if(anterior.equals("Ordua")){
				this.estropada.setOrdua(temp);
			}
		}else if(qName.equals("dict")){
			if(dict == 2){
				//System.out.println(" key: " + key + " " +  this.estropada + " " + this.le);
				this.le.put(key, this.estropada);
			}
			dict--;
		}else if(qName.equals("key")){
			Log.d("EstropadakParser", "key=" + temp);
			try{
				key = Integer.parseInt(temp);
			}catch(NumberFormatException e){
				// do nothing
			}
		}
		tagAnterior = qName;
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		// TODO Auto-generated method stub
		//super.startElement(uri, localName, qName, attributes);
		
		if(!qName.equals("dict") && !qName.equals("plist")){
			anterior = temp.trim();
			temp = "";
		}else if(qName.equals("dict")){
			dict++;
			//System.out.println("dict: " + dict + " anterior: " + anterior);
			if(dict==2){
					this.estropada = new Estropada();
			}
		}
		//System.out.print("\n<" + qName + ">|" + anterior + "|");
	}

	public static void main(String args[]) throws Exception{
		SAXParserFactory spf = SAXParserFactory.newInstance();
		spf.setValidating(false);
		SAXParser sp = spf.newSAXParser();
		String file = Thread.currentThread().getContextClassLoader().getResource("estropadak_lsm.plist").getFile();
		System.out.println("file: " + file);
		InputStream is = 
			new FileInputStream(file);
		EstropadakParser tp = new EstropadakParser();
		sp.parse(is, tp);
		System.out.println(tp.le.getStringRepresentation());
	}
	
}
